#include "FKActorsModel.h"

#include "FKDataRecord.h"

#include "FKModelRole.h"
#include "FKLogger.h"

FKActorsModel::FKActorsModel(QObject *parent):FKDataModel(parent){
    FK_CBEGIN
    FK_CEND
}

FKActorsModel::~FKActorsModel(){
    FK_DBEGIN
    FK_DEND
}

QModelIndex FKActorsModel::recordIndexForID(const int &id, FKActorsModel::RecordType rt) const{
    QList<QPersistentModelIndex> list;
    switch (rt) {
    case UserRecord:
        list=_userIndexes;
        break;
    case ActorRecord:
        list=_actorIndexes;
        break;
    default:
        return QModelIndex();
    }
    foreach (QPersistentModelIndex i, list) {
        bool t;
        qint32 recordID=data(i,FKModelRole::objectID).toInt(&t);
        if(t && recordID==id)return i;
    }
    return QModelIndex();
}

QModelIndex FKActorsModel::recordIndexForRow(const int &pos, FKActorsModel::RecordType rt) const{
    if(pos<0)return QModelIndex();
    switch (rt) {
    case UserRecord:
        if(pos<_userIndexes.size())return _userIndexes.at(pos);
        return QModelIndex();
    case ActorRecord:
        if(pos<_actorIndexes.size())return _actorIndexes.at(pos);
        return QModelIndex();
    default:
        return QModelIndex();
    }
}

qint32 FKActorsModel::actorForUserID(const qint32 &id) const{
    return data(actorIndexForUserID(id),FKModelRole::objectID).toInt();
}

QModelIndex FKActorsModel::actorIndexForUserID(const qint32 &id) const{
    QModelIndex i=recordIndexForID(id);
    return i.sibling(i.row(),FKModelElements::actors);
}

void FKActorsModel::initialization(){
    FKDataRecord* room=new FKDataRecord(0);
    FKDataList* teams=room->addElement();
        FKDataRecord* team=teams->addRecord();
            FKDataList* sharedControlGroups=team->addElement();
                FKDataRecord* sharedControlGroup=sharedControlGroups->addRecord();
                    FKDataList* users=sharedControlGroup->addElement();
                        FKDataRecord* user=users->addRecord();
                            user->setData(FKModelRole::objectID,-1);
                            user->setData(FKModelRole::nameRole,QString(tr("Open")));
                    FKDataList* actors=sharedControlGroup->addElement(FKModelElements::actors);
                        FKDataRecord* actor=actors->addRecord();
                            actor->setData(FKModelRole::objectID,-1);
                            actor->setData(FKModelRole::score,0);
        teams->cloneRecord();
    installNewSource(room);
}

void FKActorsModel::installCustomIndexes(FKDataRecord* rootRecord){
    FKDataRecord* sharedControlGroup1=rootRecord->element()->record()->element()->record();
        FKDataRecord* user1=sharedControlGroup1->element()->record();
        FKDataRecord* actor1=sharedControlGroup1->element(FKModelElements::actors)->record();
    FKDataRecord* sharedControlGroup2=rootRecord->element()->record(1)->element()->record();
        FKDataRecord* user2=sharedControlGroup2->element()->record();
        FKDataRecord* actor2=sharedControlGroup2->element(FKModelElements::actors)->record();

    _userIndexes.clear();
    _actorIndexes.clear();
    _userIndexes<<createIndex(0,FKModelElements::defaultElement,user1)<<createIndex(1,FKModelElements::defaultElement,user2);
    _actorIndexes<<createIndex(0,FKModelElements::actors,actor1)<<createIndex(1,FKModelElements::actors,actor2);
}

QVariant FKActorsModel::data(const QModelIndex &index, int role) const{
    FKDataRecord* target=record(index);
    if(target && role==FKModelRole::score)target=target->sibling(target->row(),FKModelElements::actors);
    if(target)return target->data(role);
    return QVariant();
}

bool FKActorsModel::setData(const QModelIndex &index, const QVariant &value, int role){
    FKDataRecord* target=record(index);
    if(target && role==FKModelRole::score)target=target->sibling(target->row(),FKModelElements::actors);
    if(target){
        target->setData(role,value);
        notifyDataChange(index,value,role);
        return true;
    }
    return false;
}

int FKActorsModel::rowCount(const QModelIndex &parent) const{
    if(!parent.isValid())return 2;
    return 0;
}

int FKActorsModel::columnCount(const QModelIndex &parent) const{
    if(!parent.isValid())return 2;
    return 0;
}

QModelIndex FKActorsModel::index(int row, int column, const QModelIndex &parent) const{
    if(rootRecord() && hasIndex(row,column,parent)){
        switch(column){
        case FKModelElements::defaultElement:
            return _userIndexes.at(row);
        case FKModelElements::actors:
            return _actorIndexes.at(row);
        }
    }
    return QModelIndex();
}

QModelIndex FKActorsModel::parent(const QModelIndex &child) const{
    Q_UNUSED(child)
    return QModelIndex();
}

void FKActorsModel::installNewSource(FKDataRecord *root){
    emit beginResetModel();
    setRootRecord(root);
    emit endResetModel();
    installCustomIndexes(root);
}
